package com.molichuxing.services.infrastructure.service.impl;

import org.apache.commons.lang3.StringUtils;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.services.infrastructure.bean.EsignAccountBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.EsignAccountMapper;
import com.molichuxing.services.infrastructure.dto.request.create.EsignAccountCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.EsignAccountModifyDto;
import com.molichuxing.services.infrastructure.dto.response.EsignAccountDto;
import com.molichuxing.services.infrastructure.service.EsignAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户印章 服务实现类
 * </p>
 *
 * @author zhaopenghui
 * @since 2019-10-08
 */
@Service("esignAccountService")
public class EsignAccountServiceImpl implements EsignAccountService {

    @Autowired
    EsignAccountMapper esignAccountMapper;


    /**
     * 添加
     *
     * @param esignAccountCreateDto
     * @return
     * @throws Exception
     */
    @Override
    public Integer createEsignAccount(EsignAccountCreateDto esignAccountCreateDto) throws Exception {
        if (null == esignAccountCreateDto) {
            throw new BizException("用户印章生成失败");
        }

        EsignAccountBean esignAccountBean = Convert.toEsignAccountBean(esignAccountCreateDto);
        esignAccountMapper.create(esignAccountBean);
        return esignAccountBean.getId();
    }

    /**
     * 修改
     *
     * @param esignAccountModifyDto
     * @return
     * @throws Exception
     */
    @Override
    public Boolean modifyEsignAccount(EsignAccountModifyDto esignAccountModifyDto) throws Exception {
        if (null == esignAccountModifyDto) {
            throw new BizException("用户印章生成失败");
        }

        String idNumber = esignAccountModifyDto.getIdNumber();
        if (StringUtils.isBlank(idNumber)) {
            throw new BizException("用户印章生成失败");
        }

        EsignAccountBean esignAccountBean = esignAccountMapper.getEsignByNameAndIdNumber(null, idNumber);
        if (null == esignAccountBean) {
            throw new BizException("用户印章生成失败");
        }

        Convert.toEsignAccountBean(esignAccountModifyDto, esignAccountBean);
        return (esignAccountMapper.modifyByIdNumber(esignAccountBean) > 0) ? true : false;
    }

    /**
     * 根据用户姓名身份证号查询
     *
     * @param name
     * @param idNumber
     * @return
     */
    @Override
    public EsignAccountDto getEsignByNameAndIdNumber(String name, String idNumber) {
        if (StringUtils.isBlank(name) && StringUtils.isBlank(idNumber)) {
            return null;
        }

        EsignAccountBean esignAccountBean = esignAccountMapper.getEsignByNameAndIdNumber(name, idNumber);
        if (null == esignAccountBean) {
            return null;
        }
        return Convert.toEsignAccountDto(esignAccountBean);
    }


}
